package com.base.common.db.dao;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;

import com.base.common.bean.msg.push.PushMessage;
import com.base.common.db.DBConstants;
import com.base.common.db.constant.ConstantPushMessage;
import com.base.common.util.ObjectUtil;
import com.base.common.util.StringUtils;
import com.base.common.util.log.LogUtil;

import java.io.File;
import java.util.ArrayList;


public class PushMessageDAO {
	private static final String TAG = PushMessageDAO.class.getName();
	private Context context;
	/** 表URI **/
	private static Uri urString;

	static {
		urString = Uri.parse(DBConstants.ContentProvider.PROTOCOL
				+ DBConstants.ContentProvider.AUTHORITY + File.separator
				+ ConstantPushMessage.TABLENAME);
	}

	public PushMessageDAO(Context context) {
		this.context = context;
	}

	/** 根据id获取单个数据 **/
	public PushMessage getById(String id) {
		String sql = "SELECT * FROM " + ConstantPushMessage.TABLENAME
				+ " where " + ConstantPushMessage.ID + " ='" + id + "'";
		Cursor cursor = context.getContentResolver().query(urString, null,
				"sql://" + sql, null, null);
		PushMessage OuPushTbl = null;
		if (cursor != null && cursor.moveToFirst()) {
			Object obj = null;
			try {
				obj = ObjectUtil.convertCursorToObject(cursor,
						PushMessage.class);
			} catch (IllegalArgumentException e) {
				LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
			} catch (IllegalAccessException e) {
				LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
			}
			if (obj instanceof PushMessage)
				OuPushTbl = (PushMessage) obj;
		}
		if (cursor != null)
			cursor.close();
		return OuPushTbl;
	}

	/** 保存数据 **/
	public long save(PushMessage pushMessage) {
		long result = 0;
		if (StringUtils.isNotEmpty(pushMessage.getId())) {
			PushMessage ouPushTbl = getById(pushMessage.getId());
			if (ouPushTbl != null) {
				return result;
			}
		} else {
			return result;
		}
		try {
			ContentResolver contentResolver = context.getContentResolver();
			ContentValues values = new ContentValues();
			values.put(ConstantPushMessage.ID, pushMessage.getId());
			values.put(ConstantPushMessage.MSGCONTENT,
					pushMessage.getMsgContent());
			values.put(ConstantPushMessage.MSGSHOWTIME,
					pushMessage.getMsgShowtime());
			values.put(ConstantPushMessage.MSGTARGETCONTENT,
					pushMessage.getMsgTargetcontent());
			values.put(ConstantPushMessage.MSGTARGETID,
					pushMessage.getMsgTargetid());
			values.put(ConstantPushMessage.MSGTARGETTYPE,
					pushMessage.getMsgTargettype());
			values.put(ConstantPushMessage.MSGTITLE, pushMessage.getMsgTitle());
			values.put(ConstantPushMessage.PUSHSERVICE,
					pushMessage.getPushService());
			values.put(ConstantPushMessage.DEADLINE, pushMessage.getDeadLine());
			values.put(ConstantPushMessage.STATUS, pushMessage.getStatus());
			Uri uri = contentResolver.insert(urString, values);
			result = ContentUris.parseId(uri);// /取得主健ID
		} catch (IllegalArgumentException e) {
			LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
		}
		return result;
	}

	/** 更新单个数据 **/
	public void update(PushMessage pushMessage) {
		ContentResolver contentResolver = context.getContentResolver();
		ContentValues values = new ContentValues();
		values.put(ConstantPushMessage.ID, pushMessage.getId());
		values.put(ConstantPushMessage.MSGCONTENT, pushMessage.getMsgContent());
		values.put(ConstantPushMessage.MSGSHOWTIME,
				pushMessage.getMsgShowtime());
		values.put(ConstantPushMessage.MSGTARGETCONTENT,
				pushMessage.getMsgTargetcontent());
		values.put(ConstantPushMessage.MSGTARGETID,
				pushMessage.getMsgTargetid());
		values.put(ConstantPushMessage.MSGTARGETTYPE,
				pushMessage.getMsgTargettype());
		values.put(ConstantPushMessage.MSGTITLE, pushMessage.getMsgTitle());
		values.put(ConstantPushMessage.PUSHSERVICE,
				pushMessage.getPushService());
		values.put(ConstantPushMessage.DEADLINE, pushMessage.getDeadLine());
		values.put(ConstantPushMessage.STATUS, pushMessage.getStatus());
		contentResolver.update(urString, values, ConstantPushMessage.ID + "='"
				+ pushMessage.getId() + "'", null);
	}

	/**
	 * 获取列表
	 * 
	 * @param messageId
	 * @return
	 */
	public ArrayList<PushMessage> getShowList(int status) {
		ArrayList<PushMessage> list = null;
		String sql = "SELECT * FROM " + ConstantPushMessage.TABLENAME
				+ " WHERE " + ConstantPushMessage.DEADLINE + " > '"
				+ System.currentTimeMillis() + "' AND ("
				+ ConstantPushMessage.STATUS + " = " + status + " OR "
				+ ConstantPushMessage.STATUS + " = "
				+ ConstantPushMessage.STATUS_ALWAYS + ")";
		Cursor cursor = context.getContentResolver().query(urString, null,
				"sql://" + sql, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			list = new ArrayList<PushMessage>();
			while (!cursor.isAfterLast()) {
				Object obj = null;
				try {
					obj = ObjectUtil.convertCursorToObject(cursor,
							PushMessage.class);
				} catch (IllegalArgumentException e) {
					LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
				} catch (IllegalAccessException e) {
					LogUtil.e(TAG, StringUtils.getExceptionMsg(e));
				}
				if (obj == null) {
					continue;
				}
				list.add((PushMessage) obj);
				cursor.moveToNext();
			}
		}
		if (cursor != null) {
			cursor.close();
		}
		return list;
	}

	/**
	 * 所有数量
	 * 
	 * @param messageId
	 * @param from
	 * @return
	 */
	public int getCount() {
		int count = 0;
		String where = " where " + ConstantPushMessage.DEADLINE + " <'"
				+ System.currentTimeMillis() + "'";
		String sql = "SELECT COUNT(*) FROM " + ConstantPushMessage.TABLENAME
				+ where;
		Cursor cursor = context.getContentResolver().query(urString, null,
				"sql://" + sql, null, null);
		LogUtil.i(TAG, sql);
		if (cursor != null && cursor.moveToFirst()) {
			count = cursor.getInt(0);
		}
		if (cursor != null)
			cursor.close();
		return count;

	}

	/** 删除一条数据 **/
	public int delete(String id) {
		int result = 0;
		ContentResolver contentResolver = context.getContentResolver();
		result = contentResolver.delete(urString, ConstantPushMessage.ID + "='"
				+ id + "'", null);
		return result;
	}

	/** 监听表的数据变化 **/
	public void observer(ContentObserver contentObserver) {
		context.getContentResolver().registerContentObserver(urString, true,
				contentObserver);
	}
	
	/**
         * 取消监听
         * @param contentObserver
         */
        public void unObserver(ContentObserver contentObserver) {
            context.getContentResolver().unregisterContentObserver(contentObserver);
        }

	/** 清空表 **/
	public int deleteAll() {
		int result = 0;
		ContentResolver contentResolver = context.getContentResolver();
		result = contentResolver.delete(urString, null, null);
		return result;
	}
}
